﻿using System.Web;
using WebService.Security;

namespace WebService.Web {

    /// <summary>
    /// A secure control enforces passport being supplied..
    /// </summary>
    public abstract class AbstractControl : IHttpHandler {

        protected abstract void Process(UserToken user, HttpRequest request, HttpResponse response);
        
        public bool IsReusable {
            get {
                return true;
            }
        }
        
        public void ProcessRequest(HttpContext context) {
            var request  = context.Request;
            var response = context.Response;
            var passport = request.Params.Get("passport");
            if (string.IsNullOrEmpty(passport)) {
                var cookie = request.Cookies.Get("passport");
                passport = cookie != null ? cookie.Value : "";
            }

            var user = PassportUtil.ReadPassport(passport);
            Process(user, request, response);
        }

    }
}